/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 悬浮播放器初始化测试
 */

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';

import '../../lib/services/floating_ui_manager.dart';
import '../../lib/services/floating_player_service.dart';
import '../../lib/viewmodels/music_player_view_model.dart';

void main() {
  group('悬浮播放器初始化测试', () {
    late MusicPlayerViewModel musicPlayerViewModel;

    setUp(() {
      musicPlayerViewModel = MusicPlayerViewModel();
    });

    tearDown(() {
      musicPlayerViewModel.dispose();
      FloatingPlayerManager.dispose();
    });

    testWidgets('正确的初始化顺序测试', (WidgetTester tester) async {
      await tester.pumpWidget(
        ChangeNotifierProvider.value(
          value: musicPlayerViewModel,
          child: MaterialApp(
            home: FloatingPlayerInitializer(
              child: Scaffold(
                body: Builder(
                  builder: (context) {
                    return ElevatedButton(
                      onPressed: () {
                        FloatingPlayerService.initialize(context);
                      },
                      child: const Text('初始化服务'),
                    );
                  },
                ),
              ),
            ),
          ),
        ),
      );

      // 等待初始化完成
      await tester.pumpAndSettle();

      // 点击初始化按钮
      await tester.tap(find.text('初始化服务'));
      await tester.pumpAndSettle();

      // 验证没有抛出异常
      expect(tester.takeException(), isNull);
    });

    testWidgets('重复初始化测试', (WidgetTester tester) async {
      await tester.pumpWidget(
        ChangeNotifierProvider.value(
          value: musicPlayerViewModel,
          child: MaterialApp(
            home: FloatingPlayerInitializer(
              child: Scaffold(
                body: Builder(
                  builder: (context) {
                    return Column(
                      children: [
                        ElevatedButton(
                          onPressed: () {
                            FloatingPlayerService.initialize(context);
                          },
                          child: const Text('第一次初始化'),
                        ),
                        ElevatedButton(
                          onPressed: () {
                            FloatingPlayerService.initialize(context);
                          },
                          child: const Text('第二次初始化'),
                        ),
                      ],
                    );
                  },
                ),
              ),
            ),
          ),
        ),
      );

      // 等待初始化完成
      await tester.pumpAndSettle();

      // 第一次初始化
      await tester.tap(find.text('第一次初始化'));
      await tester.pumpAndSettle();

      // 第二次初始化（应该跳过）
      await tester.tap(find.text('第二次初始化'));
      await tester.pumpAndSettle();

      // 验证没有抛出异常
      expect(tester.takeException(), isNull);
    });
  });
}
